package com.dinglian.ots.controller.backstage;

import com.dinglian.ots.common.model.Constant;
import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.shiro.MyShiroToken;
import com.dinglian.ots.vo.user.LoginByPasswordVo;
import com.dinglian.ots.vo.user.UserVo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author wangbo
 */
@Controller
@RequestMapping("/admin/")
public class WebController {

    @RequestMapping("login")
    public String toLogin() {
        return "login";
    }

    @RequiresAuthentication
    @RequestMapping("index")
    public String toIndex() {
        return "index";
    }

    @ApiOperation(value = "超级管理员登录")
    @PostMapping("doLogin")
    public String doLogin(LoginByPasswordVo vo, Model model, HttpSession session, HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        String password = vo.getPassword();
//        password = DigestUtil.md5Hex(password);
        MyShiroToken token = new MyShiroToken(vo.getUsername(), password,Constant.ADMIN_ROLE);
        try {
            subject.login(token);
            UserVo userVo = (UserVo) subject.getPrincipal();
            if (!Constant.ADMIN_ROLE_NAME.equals(userVo.getRoleName())){
                return "login";
            }
            session.setAttribute(Constant.SESSION_USER, userVo);
            return "redirect:../admin/index";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", CodeMsg.USERNAME_IS_NOT_EXIST.getMsg());
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", CodeMsg.PASSWORD_ERROR.getMsg());
        }
        return "login";
    }

    @RequiresAuthentication
    @RequestMapping("toExamineBuyer")
    public String toExamineBuyer() {
        return "examineBuyer";
    }

    @RequiresAuthentication
    @RequestMapping("toBrandRank")
    public String toBrandRank() {
        return "brandRank";
    }

    @RequiresAuthentication
    @RequestMapping("toProductRank")
    public String toProductRank() {
        return "productRank";
    }

    @RequiresAuthentication
    @RequestMapping("toTopBrandRank")
    public String toTopBrandRank() {
        return "topBrandRank";
    }

}
